package com.example.myapplication

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.myapplication.ui.theme.MyApplicationTheme
import com.example.myapplication.ui.theme.LakersPurple
import com.example.myapplication.ui.theme.LakersGold

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            MyApplicationTheme {
                var isLoggedIn by remember { mutableStateOf(false) }
                var currentUserToken by remember { mutableStateOf("") }
                var showRegisterScreen by remember { mutableStateOf(false) }
                
                when {
                    showRegisterScreen -> {
                        // 显示注册界面
                        RegisterScreen(
                            onRegisterSuccess = { token ->
                                currentUserToken = token
                                isLoggedIn = true
                                showRegisterScreen = false
                            },
                            onNavigateToLogin = { 
                                showRegisterScreen = false
                            }
                        )
                    }
                    isLoggedIn -> {
                        // 登录成功后显示主界面
                        MainScreen(
                            name = "Android",
                            token = currentUserToken,
                            onLogout = { 
                                isLoggedIn = false
                                currentUserToken = ""
                            }
                        )
                    }
                    else -> {
                        // 显示登录界面
                        LoginScreen(
                            onLoginSuccess = { token ->
                                currentUserToken = token
                                isLoggedIn = true
                            },
                            onNavigateToRegister = { 
                                showRegisterScreen = true
                            }
                        )
                    }
                }
            }
        }
    }
}

@Composable
fun MainScreen(
    name: String,
    token: String,
    modifier: Modifier = Modifier,
    onLogout: () -> Unit = {},
    context: android.content.Context = androidx.compose.ui.platform.LocalContext.current
) {
    // 创建渐变背景
    val gradientBackground = Brush.linearGradient(
        colors = listOf(
            LakersPurple.copy(alpha = 0.7f),
            LakersGold.copy(alpha = 0.3f),
            LakersPurple.copy(alpha = 0.7f)
        )
    )
    
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(gradientBackground)
    ) {
        // 内容区域
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(16.dp)
                .verticalScroll(rememberScrollState()),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            // 卡片容器
            Card(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(16.dp)
                    .shadow(8.dp),
                shape = RoundedCornerShape(16.dp)
            ) {
                Column(
                    modifier = Modifier
                        .padding(24.dp)
                        .fillMaxWidth(),
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    // 标题
                    Text(
                        text = "欢迎使用",
                        style = MaterialTheme.typography.headlineMedium,
                        color = LakersPurple,
                        fontWeight = FontWeight.Bold,
                        modifier = Modifier.padding(bottom = 8.dp)
                    )
                    
                    Text(
                        text = "湖人主题应用",
                        style = MaterialTheme.typography.titleMedium,
                        color = LakersGold,
                        modifier = Modifier.padding(bottom = 24.dp)
                    )
                    
                    // 欢迎信息
                    Text(
                        text = "欢迎, $name!",
                        style = MaterialTheme.typography.headlineSmall,
                        modifier = Modifier.padding(bottom = 16.dp)
                    )
                    
                    Text(
                        text = "您已成功登录到湖人主题应用",
                        style = MaterialTheme.typography.bodyLarge,
                        modifier = Modifier.padding(bottom = 16.dp)
                    )
                    
                    Text(
                        text = "Token: ${token.take(20)}...",
                        style = MaterialTheme.typography.bodyMedium,
                        modifier = Modifier.padding(bottom = 32.dp)
                    )

                    // 退出登录按钮
                    Button(
                        onClick = onLogout,
                        modifier = Modifier
                            .fillMaxWidth(),
                        shape = RoundedCornerShape(8.dp),
                        colors = ButtonDefaults.buttonColors(
                            containerColor = LakersPurple,
                            contentColor = Color.White
                        )
                    ) {
                        Text("退出登录", fontWeight = FontWeight.Bold)
                    }
                }
            }
        }
    }
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    MyApplicationTheme {
        MainScreen("Android", "sample_token")
    }
}